package traceability

import (
	"context"
	"encoding/json"
	"group-card/common/response"
	"group-card/rpc/card/cardclient"

	"group-card/api/internal/svc"
	"group-card/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type TraceabilityFindOneLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewTraceabilityFindOneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TraceabilityFindOneLogic {
	return &TraceabilityFindOneLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TraceabilityFindOneLogic) TraceabilityFindOne(req *types.FindOneTraceabilityRequest) (resp *types.TraceabilityResponse, err error) {
	data, err := l.svcCtx.Card.TraceabilityFindOne(l.ctx, &cardclient.TraceabilityFindOneRequest{
		Id:         req.Id,
		CardNumber: req.CardNumber,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("查询卡片溯源详情失败,参数:%s,异常:%s", reqStr, err.Error())
		return &types.TraceabilityResponse{Code: response.ErrCode, Message: "查询卡片溯源失败"}, nil
	}

	return &types.TraceabilityResponse{
		Code:    response.SuccessCode,
		Message: "查询卡片溯源成功",
		Data:    data,
	}, nil
}
